//
//  giftStorageView.m
//  tcpoctest
//
//  Created by gump on 6/5/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "giftStorageView.h"

#import "giftStroageItemView.h"

#import "giftStroageDataSource.h"
#import "GiftInfoDataSource.h"

#import "giftStroageDataInfo.h"
#import "GiftInfoDataInfo.h"

#import "sendGiftListLength.h"
#import "sendPersonReadCoin.h"
#import "sendGiftGetStroage.h"

@implementation giftStorageView

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        LIGiftViewDic = [[NSMutableDictionary alloc] init];
    }
    
    return self;
}


-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"他的礼物";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回"
                                                                target:self
                                                                action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    UIBarButtonItem *nextItem = [self createBarButtonItemWithTitle:@"送礼"
                                                            target:self
                                                            action:@selector(nextButtonPressed)];
    self.navItem.rightBarButtonItem = nextItem;
    
    LIScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
                                                                  TOPBAR_HIGHT,
                                                                  WINDOW_WIDTH,
                                                                  WINDOW_HIGTH - TOPBAR_HIGHT * 2)];
    
    [self.view addSubview:LIScrollView];
}

#pragma mark -private fun-

-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)nextButtonPressed
{
    [DataControl shareControl].giftInfoDS.strSenderMail =
    [DataControl shareControl].currentFI.mail;
    
    [[DataControl shareControl].giftInfoDS SetShowArrByListID:1];
    
    [[ViewManager sharedViewManager] showView:VI_GIFTLISTINFO
                             withIsRemoveCurV:false
                                withDirection:VD_RIGHT];
    
    [[netManager sharedNetManager].sendgiftLL sendGiftLL:1];
    
    [[netManager sharedNetManager].sendPersonRC sendReadCoin];
}

-(void)refreshView
{
    int icount = [[DataControl shareControl].giftStroageDS GetArrCount];
    
    CGSize newSize = CGSizeMake(WINDOW_WIDTH, (icount/4 + 1) * GIFT_VIEW_HIGHT);
    [LIScrollView setContentSize:newSize];
    
    NSMutableArray *showarr =  [NSArray arrayWithArray:
                                [[DataControl shareControl].giftStroageDS GetGSArr]];
    
    if (showarr != nil) {
        for (giftStroageDataInfo *giftid in showarr){
            giftStroageItemView *giv = [LIGiftViewDic objectForKey:giftid.strgiftid];
            
            if (giv == nil) {
                
                giv = [self creatGiftItemViewByGiftID:giftid];
                
                if (giv != nil) {
                    [LIGiftViewDic setObject:giv forKey:giftid.strgiftid];
                    
                    [self addGiftView:giv withDicCount:LIGiftViewDic.count];
                }
                
            }
            else
            {
                [giv ReSetImageByID:giftid.strgiftid];
            }
        }
    }
}

-(void)viewWillShow
{
    NSArray *gvarr = [LIGiftViewDic allValues];
    for (giftStroageItemView *giv in gvarr) {
        if (giv != nil) {
            [giv removeFromSuperview];
        }
    }
    
    if ([[DataControl shareControl].giftStroageDS GetArrCount] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"赠送礼物"
                                                        message:@"他还没有礼物,快给他送礼吧!"
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"送礼",nil];
        [alert show];
    }
    
    [LIGiftViewDic removeAllObjects];
    
    
    [self refreshView];
    
}

-(giftStroageItemView *)creatGiftItemViewByGiftID:(giftStroageDataInfo *)gsinfo
{
    giftStroageItemView *giv = [[giftStroageItemView alloc] initWithFrame:
                         CGRectMake(0, 0, GIFT_VIEW_WIDTH, GIFT_VIEW_HIGHT)];
    
    giv.gsinfo = gsinfo;
    [giv initUI];
    
    GiftInfoDataInfo *ginfo = [[DataControl shareControl].giftInfoDS getGiftInfoByID:gsinfo.strgiftid];
    
    if (ginfo != nil) {
        
        [giv ReSetImageByID:gsinfo.strgiftid];
        
        giv.giftLabel.text = [NSString stringWithFormat:@"x %@",gsinfo.strGiftCount];
        
        if (ginfo.giftIsMusic == 2) {
            giv.giftLabel.textColor = [UIColor redColor];
        }
    }
    
    return giv;
}

-(void)addGiftView:(giftStroageItemView *)giv withDicCount:(int)icount
{
    int row = (icount - 1) % 4;
    
    int line = (icount - 1) / 4;
    
    giv.frame = CGRectMake(GIFT_VIEW_WIDTH * row,
                           GIFT_VIEW_HIGHT * line,
                           GIFT_VIEW_WIDTH,
                           GIFT_VIEW_HIGHT);
    
    [LIScrollView addSubview:giv];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        if ([DataControl shareControl].currentFI == nil) {
            return;
        }
        
        [DataControl shareControl].giftInfoDS.strSenderMail =
        [DataControl shareControl].currentFI.mail;
        
        [[DataControl shareControl].giftInfoDS SetShowArrByListID:1];
        
        [[ViewManager sharedViewManager] showView:VI_GIFTLISTINFO
                                 withIsRemoveCurV:false
                                    withDirection:VD_RIGHT];
        
        [[netManager sharedNetManager].sendgiftLL sendGiftLL:1];
        
        [[netManager sharedNetManager].sendPersonRC sendReadCoin];
        
    }
}

@end
